<?php

namespace App\Models\Activity;

use App\Models\ActivityPrize\ActivityPrize;
use App\Models\ActivityRecord\ActivityRecord;
use App\Models\Groupable;
use App\Models\VoteGroup\VoteGroup;
use App\Support\Database\Eloquent\UuidModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\ActivityModel\ActivityModel;
use App\Models\ActivityModelTemplate\ActivityModelTemplate;

class Activity extends UuidModel
{
    use SoftDeletes, Groupable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    const ACTIVITY_CATEGORY_WECHAT = 1;
    const ACTIVITY_CATEGORY_H5 = 2;
    const ACTIVITY_LIMIT_DAY_TYPE = 'day';
    const ACTIVITY_LIMIT_ALL_TYPE = 'all';

    const PAUSED = 0;
    const RUNNING = 1;

    // 暂时不使用casts, 为了兼容以前的调用，甩不掉的历史包袱
    /*
    protected $casts = [
        'rule' => 'json',
        'limit' => 'json',
        'shares' => 'json',
    ];
    */

    protected $fillable = [
        'id',
        'user_id',
        'category',
        'model_id',
        'template_id',
        'isPause',
        'clicks',
        'joins',
        'winnings',
        'name',
        'startTime',
        'endTime',
        'limit',
        'description',
        'ruleDescription',
        'listBackgroundImg',
        'shares',
        'rule',
        'group_code',
        'app_url',
        'publishing_ways',
        'remark',
        'organizer',
        'others',
        'customId',
    ];

    protected $dates = ['deleted_at'];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var arrays
     */
    protected $hidden = [];

    public function model()
    {
        return $this->belongsTo(ActivityModel::class, 'model_id');
    }

    public function template()
    {
        return $this->belongsTo(ActivityModelTemplate::class, 'template_id');
    }

    public function prizes()
    {
        return $this->hasMany(ActivityPrize::class, 'activity_id');
    }

    public function records(){
        return $this->hasMany(ActivityRecord::class, 'activity_id');
    }

    public function groups()
    {
        return $this->hasMany(VoteGroup::class, 'activity_id');
    }

    /**
     * @return string|null
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return bool
     */
    public function isExpire()
    {
        if (strtotime($this->endTime) < time()) {
            return true;
        }

        return false;
    }

    /**
     * @return bool
     */
    public function isPause()
    {
        return ($this->isPause == self::PAUSED) ? true : false;
    }

    public function appIsOpen()
    {
        $publishingWays = json_decode($this->publishing_ways, true);
        if(isset($publishingWays['app']) && $publishingWays['app']['is_open'] == 'on'){
            return true;
        }

        return false;
    }

    public function wechatIsOpen()
    {
        $publishingWays = json_decode($this->publishing_ways, true);
        if(isset($publishingWays['wechat']) && $publishingWays['wechat']['is_open'] == 'on'){
            return true;
        }

        return false;
    }
}
